package com.llkj.qingnianguoxue.music;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.RecommendMusicAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.CourseEvaluateBean;
import com.llkj.qingnianguoxue.bean.MusicHomeBean;
import com.llkj.qingnianguoxue.bean.MusicLikeBean;
import com.llkj.qingnianguoxue.bean.response.ResponseList;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MusicAllActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener{
    @BindView(R.id.refresh_layout)
    protected SwipeRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    protected RecyclerView rlMusic;
    private RecommendMusicAdapter mMusicAdapter;
    private List<CourseEvaluateBean> mMusicData;
    private int totalSize, mPage;
    private int mCurrentPosition;//当前位置

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_all);
        ButterKnife.bind(this);
        setTitle("全部曲库", true, TITLE_TYPE_IMG, R.drawable.icon_back, false,-1,-1);

        initData();
        initListener();

        loadWaitDialog();
        onRefresh();
    }

    private void initData(){
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        rlMusic.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false));
        mMusicData = new ArrayList<>();
        mMusicAdapter = new RecommendMusicAdapter(R.layout.item_recommend_music,mMusicData);
        rlMusic.setAdapter(mMusicAdapter);
    }

    private void initListener(){
        refreshLayout.setOnRefreshListener(this);
        mMusicAdapter.setOnLoadMoreListener(this);

        rlMusic.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                mCurrentPosition = i;
                CourseEvaluateBean item = mMusicData.get(mCurrentPosition);
                if(view.getId()== R.id.btnSing){
                    RecordMusicActivity.startActivity(MusicAllActivity.this,item.getId(),item.getSing_img()
                            ,item.getName(),item.getPlay_num(),item.getAccompaniment(),item.getMp3(),item.getLyric(),item.getSing_name());
                }else if(view.getId() == R.id.ivPraise){//喜欢
//                    loadWaitDialog();
                    String type = "1".equals(item.getIs_like())?"2":"1";
                    HttpMethod.kojiUserOperation(MusicAllActivity.this,MusicAllActivity.this,item.getId(),type,"2");
                }
            }
        });
        rlMusic.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                CourseEvaluateBean item = mMusicData.get(i);
                Bundle bundle  = new Bundle();
                bundle.putString("musicId",item.getId());
                startActivity(MusicDetailActivity.class,bundle);
            }
        });
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getData();
    }
    @Override
    public void onLoadMore() {
        mPage++;
        getData();
    }

    private void getData(){
        HttpMethod.kojiWhole(this,this,String.valueOf(mPage));
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, mMusicAdapter, null, rlMusic, totalSize);
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
//        refreshLayout.setRefreshing(false);
        if (HttpMethod.HTTP_KOJI_WHOLE == method) {//
            ResponseList<CourseEvaluateBean> response = GsonUtil.fromJsonList(data,CourseEvaluateBean.class);
            if(response!=null){
                totalSize = Integer.parseInt(response.getNum());
                if (mPage == 1) {
                    mMusicData.clear();
                    LoadDataHelper.refreshComplete(mMusicAdapter, mMusicData, refreshLayout, rlMusic);
                } else {
                    mMusicAdapter.setEnableLoadMore(true);
                    mMusicAdapter.loadMoreComplete();
                }
                mMusicData.addAll(response.getKoji());
                mMusicAdapter.notifyDataSetChanged();
            }
        }else if(HttpMethod.HTTP_KOJI_USER_OPERATION == method){
            CourseEvaluateBean item = mMusicData.get(mCurrentPosition);
            String type = "1".equals(item.getIs_like())?"2":"1";
            item.setIs_like(type);
            mMusicAdapter.notifyItemChanged(mCurrentPosition);
        }
    }
}
